//
//  MTCategoryViewController.m
//  美团HD
//
//  Created by kongxiaopeng on 15/6/18.
//  Copyright (c) 2015年 njsk. All rights reserved.
//

#import "MTCategoryViewController.h"
#import "MTHomeDropdown.h"
#import "MJExtension.h"
#import "MTCategory.h"
#import "UIView+Extension.h"
#import "MTMetaTool.h"
#import "MTConst.h"
@interface MTCategoryViewController ()<MTHomeDropdownDataSource,MTHomeDropdownDelegate>

@end

@implementation MTCategoryViewController

- (void)loadView{
    MTHomeDropdown* dropdown = [MTHomeDropdown dropdown];
    //加载分类数据
    dropdown.dataSource = self;
    dropdown.delegate = self;
    self.view = dropdown;
    
    
    //设置控制器view在popover中得尺寸
    self.preferredContentSize = dropdown.size;
    // iPad中控制器的view的尺寸默认都是1024x768, MTHomeDropdown的尺寸默认是300x340
    // MTCategoryViewController显示在popover中,尺寸变为480x320, MTHomeDropdown的尺寸也跟着减小:0x0
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - MTHomeDropdownDataSource
-(NSInteger)numberOfRowsInMainTable:(MTHomeDropdown *)homeDropdown{
    return [MTMetaTool categories].count;
}

-(NSString *)homeDropdown:(MTHomeDropdown *)homeDropdown titleForRowInMainTable:(NSInteger)row{
    MTCategory* category = [MTMetaTool categories][row];
    return category.name;
}

-(NSString *)homeDropdown:(MTHomeDropdown *)homeDropdown iconForRowInMainTable:(NSInteger)row{
    MTCategory* category = [MTMetaTool categories][row];
    return category.small_icon;
}

-(NSString *)homeDropdown:(MTHomeDropdown *)homeDropdown selectedIconForRowInMainTable:(NSInteger)row{
    MTCategory* category = [MTMetaTool categories][row];
    return category.small_highlighted_icon;
}

-(NSArray *)homeDropdown:(MTHomeDropdown *)homeDropdown subdataForRowInMainTable:(NSInteger)row{
    MTCategory* category = [MTMetaTool categories][row];
    return category.subcategories;
}

#pragma mark - MTHomeDropdownDelegate
-(void)homeDropdown:(MTHomeDropdown *)homeDropdown didSelectRowInMainTable:(NSInteger)row{
    MTCategory* category = [MTMetaTool categories][row];
    if (category.subcategories.count == 0) {
        //发出通知
        [MTNotificationCenter postNotificationName:MTCategoryDidChangeNotification object:nil userInfo:@{MTSelectCategory:category}];
    }
}

-(void)homeDropdown:(MTHomeDropdown *)homeDropdown didSelectRowInSubTable:(NSInteger)subrow inMainTable:(NSInteger)mainrow{
    MTCategory* category = [MTMetaTool categories][mainrow];
    //发出通知
    [MTNotificationCenter postNotificationName:MTCategoryDidChangeNotification object:nil userInfo:@{MTSelectCategory:category,MTSelectSubcategoryName:category.subcategories[subrow]}];
}
@end
